package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.corona.Game;
import com.corona.game.util.SpriteProxy;
import com.corona.game.util.TextureCache;
import com.corona.game.util.audio.AudioLoader;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;
import org.lwjgl.system.windows.User32;

/* compiled from: Action.java */
/* access modifiers changed from: package-private */
/* loaded from: pokewilds.jar:com/pkmngen/game/DisplayText.class */
public class DisplayText extends Action {
    ArrayList<Sprite> spritesNotDrawn;
    ArrayList<Sprite> spritesBeingDrawn;
    Sprite arrowSprite;
    Action playSoundAction;
    boolean playSound;
    Action scrollUpAction;
    public Action.Layer layer;
    Sprite helperSprite;
    Sprite bgSprite;
    int timer;
    int speedTimer;
    int speed;
    Action triggerAction;
    boolean foundTrigger;
    boolean checkTrigger;
    boolean persist;
    boolean waitInput;
    public boolean braillify;
    boolean firstStep;
    Texture unownTiles;
    public static boolean textPersist = false;
    public static boolean unownText = false;
    public static final Pattern DIACRITICS_AND_FRIENDS = Pattern.compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");

    private static String stripDiacritics(String str) {
        return DIACRITICS_AND_FRIENDS.matcher(Normalizer.normalize(str, Normalizer.Form.NFD)).replaceAll("");
    }

    public DisplayText(Game game, String textString, String playSound, Action triggerAction, Action nextAction) {
        this(game, textString, playSound, triggerAction, false, nextAction);
    }

    public DisplayText(Game game, String textString, String playSound, Action triggerAction, boolean braillify, Action nextAction) {
        super(new Object[0]);
        String lines;
        SpriteProxy letterSprite;
        this.layer = Action.Layer.gui_105;
        this.persist = false;
        this.waitInput = true;
        this.braillify = false;
        this.nextAction = nextAction;
        this.firstStep = true;
        this.triggerAction = triggerAction;
        this.foundTrigger = false;
        this.checkTrigger = false;
        this.braillify = braillify;
        this.spritesNotDrawn = new ArrayList<>();
        this.spritesBeingDrawn = new ArrayList<>();
        if (playSound != null) {
            this.playSoundAction = new PlaySoundText(playSound, null);
            this.playSound = true;
        } else {
            this.playSound = false;
        }
        this.speed = Game.textSpeed.framesToWait;
        this.speedTimer = this.speed;
        String line = "";
        String lines2 = "";
        String[] words = textString.split(" ");
        for (String word : words) {
            if (line.length() + word.length() < 19) {
                line = line + word;
                if (line.length() != 18) {
                    line = line + " ";
                }
            } else {
                while (line.length() < 18) {
                    line = line + " ";
                }
                lines2 = lines2 + line;
                lines2 = this.braillify ? lines2 + line : lines2;
                line = word + " ";
            }
        }
        if (this.braillify) {
            while (line.length() < 18) {
                line = line + " ";
            }
            lines = (lines2 + line) + line;
        } else {
            lines = lines2 + line;
        }
        char[] textArray = lines.toCharArray();
        this.unownTiles = TextureCache.get(Gdx.files.internal("unown_font.png"));
        int i = 0;
        int j = 0;
        for (char letter : textArray) {
            if (!unownText) {
                letterSprite = game.textDict.get(Character.valueOf(letter));
                if (letterSprite == null) {
                    letterSprite = game.textDict.get(null);
                }
            } else {
                letter = stripDiacritics(Character.toString(letter)).charAt(0);
                if (letter == '!') {
                    letterSprite = new Sprite(this.unownTiles, 260, 0, 10, 9);
                } else if (letter == '?') {
                    letterSprite = new Sprite(this.unownTiles, (int) User32.WM_IME_ENDCOMPOSITION, 0, 10, 9);
                } else if (Character.isAlphabetic(letter)) {
                    if (Character.isLowerCase(letter)) {
                        letterSprite = new Sprite(this.unownTiles, 10 * (letter - 'a'), 0, 10, 9);
                    } else {
                        letterSprite = new Sprite(this.unownTiles, 10 * (letter - 'A'), 0, 10, 9);
                    }
                } else if (letter == ' ') {
                    letterSprite = game.textDict.get(' ');
                } else {
                    letterSprite = game.textDict.get(null);
                }
            }
            Sprite currSprite = new Sprite(letterSprite);
            if (this.braillify && j % 2 == 0) {
                Sprite letterSprite2 = game.brailleDict.get(Character.valueOf(letter));
                currSprite = new Sprite(letterSprite2 == null ? game.textDict.get(' ') : letterSprite2);
            }
            if (0 == 0) {
                currSprite.setPosition((float) (((10 + (8 * i)) + 2) - 4), (float) (((26 - (16 * j)) + 2) - 4));
                this.spritesNotDrawn.add(currSprite);
                if (i >= 17) {
                    i = 0;
                    j++;
                } else {
                    i++;
                }
            }
        }
        this.arrowSprite = new Sprite(TextureCache.get(Gdx.files.internal("arrow_down.png")), 0, 0, 7, 5);
        this.arrowSprite.setPosition(144.0f, 10.0f);
        this.timer = 0;
        this.bgSprite = new Sprite(TextureCache.get(Gdx.files.internal("textbox_bg1.png")), 0, 0, 160, 144);
    }

    public DisplayText(Game game, String textString, String playSound, boolean textPersist2, Action nextAction) {
        this(game, textString, playSound, textPersist2, true, nextAction);
    }

    public DisplayText(Game game, String textString, String playSound, boolean textPersist2, boolean waitInput, boolean braillify, Action nextAction) {
        this(game, textString, playSound, (Action) null, braillify, nextAction);
        this.persist = textPersist2;
        this.waitInput = waitInput;
    }

    public DisplayText(Game game, String textString, String playSound, boolean textPersist2, boolean waitInput, Action nextAction) {
        this(game, textString, playSound, textPersist2, waitInput, false, nextAction);
    }

    @Override // com.corona.game.Action
    public String getCamera() {
        return "gui";
    }

    @Override // com.corona.game.Action
    public Action.Layer getLayer() {
        return this.layer;
    }

    @Override // com.corona.game.Action
    public void step(Game game) {
        if (this.firstStep) {
            game.displayTextAction = this;
            this.firstStep = false;
            if (this.persist) {
                textPersist = true;
            }
        }
        if (!game.uiBatch.isDrawing()) {
            game.uiBatch.begin();
        }
        this.bgSprite.draw(game.uiBatch);
        Iterator<Sprite> it = this.spritesBeingDrawn.iterator();
        while (it.hasNext()) {
            Sprite sprite = it.next();
            if (sprite.getY() <= 38.0f && sprite.getY() >= 6.0f) {
                sprite.draw(game.uiBatch);
            }
        }
        if (game.actionStack.contains(this.scrollUpAction) || game.actionStack.contains(this.playSoundAction)) {
            return;
        }
        if (this.checkTrigger || (this.persist && !textPersist)) {
            if (this.triggerAction == null && !textPersist) {
                game.actionStack.remove(this);
            } else if (game.actionStack.contains(this.triggerAction)) {
                this.foundTrigger = true;
            } else if (this.foundTrigger) {
                game.actionStack.remove(this);
            }
        } else if (this.spritesBeingDrawn.size() >= 37 || this.spritesNotDrawn.isEmpty()) {
            if (this.playSound && this.spritesNotDrawn.isEmpty()) {
                game.insertAction(this.playSoundAction);
                this.playSoundAction.step(game);
                this.playSound = false;
            } else if (this.triggerAction != null || (this.spritesNotDrawn.isEmpty() && textPersist && !this.waitInput)) {
                game.insertAction(this.nextAction);
                this.checkTrigger = true;
            } else {
                if (this.timer <= 0) {
                    if (this.timer <= -35) {
                        this.timer = 33;
                    } else {
                        this.arrowSprite.draw(game.uiBatch);
                    }
                }
                this.timer--;
                if (InputProcessor.aJustPressed) {
                    Action playSound = new PlayMusic("click1", (Action) null);
                    game.insertAction(playSound);
                    playSound.step(game);
                    if (!this.spritesNotDrawn.isEmpty()) {
                        this.scrollUpAction = new ScrollTextUp(game, this.spritesBeingDrawn, this.spritesNotDrawn, this.braillify);
                        game.insertAction(this.scrollUpAction);
                    } else if (textPersist) {
                        game.insertAction(this.nextAction);
                        this.checkTrigger = true;
                    } else {
                        game.insertAction(this.nextAction);
                        game.actionStack.remove(this);
                    }
                }
            }
        } else if (this.speedTimer > 0) {
            this.speedTimer--;
        } else {
            this.speedTimer = this.speed;
            if (this.speed == 0) {
                extract(5);
            } else if (InputProcessor.aPressed || InputProcessor.bPressed) {
                extract(3);
            } else {
                extract(1);
            }
        }
    }

    private void extract(int amount) {
        for (int i = 0; i < amount && !this.spritesNotDrawn.isEmpty(); i++) {
            this.spritesBeingDrawn.add(this.spritesNotDrawn.remove(0));
            if (this.braillify && !this.spritesNotDrawn.isEmpty() && 35 - this.spritesBeingDrawn.size() >= 0) {
                this.spritesBeingDrawn.add(this.spritesNotDrawn.remove(35 - this.spritesBeingDrawn.size()));
            }
        }
    }

    /* compiled from: Action.java */
    /* access modifiers changed from: package-private */
    /* loaded from: pokewilds.jar:com/pkmngen/game/DisplayText$Clear.class */
    public static class Clear extends Action {
        public Clear(Game game, Action nextAction) {
            super(new Object[0]);
            this.nextAction = nextAction;
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            DisplayText.textPersist = false;
            game.actionStack.remove(this);
            game.insertAction(this.nextAction);
        }
    }

    /* compiled from: Action.java */
    /* access modifiers changed from: package-private */
    /* loaded from: pokewilds.jar:com/pkmngen/game/DisplayText$PlaySoundText.class */
    public class PlaySoundText extends Action {
        Music music;
        float initialVolume;
        boolean playedYet = false;

        /* JADX INFO: 'super' call moved to the top of the method (can break code semantics) */
        public PlaySoundText(String sound, Action nextAction) {
            super(new Object[0]);
            DisplayText.this = this$0;
            this.nextAction = nextAction;
            String path = "";
            float volume = 1.0f;
            if (sound.contains("cries")) {
                volume = 0.5f;
            } else if (!sound.contains("attacks")) {
                path = "sounds/";
            }
            this.music = AudioLoader.loadMusic(path + sound);
            this.music.setLooping(false);
            this.music.setVolume(volume);
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            if (this.music != null && !this.playedYet) {
                this.initialVolume = game.currMusic.getVolume();
                game.currMusic.setVolume(0.0f);
                this.music.play();
                this.playedYet = true;
            }
            if (!this.music.isPlaying()) {
                game.currMusic.setVolume(this.initialVolume);
                game.actionStack.remove(this);
                game.insertAction(this.nextAction);
            }
        }
    }

    /* compiled from: Action.java */
    /* loaded from: pokewilds.jar:com/pkmngen/game/DisplayText$ScrollTextUp.class */
    class ScrollTextUp extends Action {
        ArrayList<Vector2> positions;
        Vector2 position;
        ArrayList<Sprite> text;
        ArrayList<Sprite> otherText;
        boolean braillify;
        int numRemove;
        public Action.Layer layer;

        public ScrollTextUp(DisplayText this$0, Game game, ArrayList<Sprite> text, ArrayList<Sprite> otherText) {
            this(game, text, otherText, false);
        }

        /* JADX INFO: 'super' call moved to the top of the method (can break code semantics) */
        public ScrollTextUp(Game game, ArrayList<Sprite> text, ArrayList<Sprite> otherText, boolean braillify) {
            super(new Object[0]);
            DisplayText.this = this$0;
            this.braillify = false;
            this.numRemove = 18;
            this.layer = Action.Layer.gui_110;
            this.text = text;
            this.otherText = otherText;
            this.braillify = braillify;
            this.positions = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                this.positions.add(new Vector2(0.0f, 0.0f));
            }
            this.positions.add(new Vector2(0.0f, 8.0f));
            for (int i2 = 0; i2 < 5; i2++) {
                this.positions.add(new Vector2(0.0f, 0.0f));
            }
            this.positions.add(new Vector2(0.0f, 8.0f));
            if (this.braillify) {
                this.numRemove = 36;
            }
        }

        @Override // com.corona.game.Action
        public String getCamera() {
            return "gui";
        }

        @Override // com.corona.game.Action
        public Action.Layer getLayer() {
            return this.layer;
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            this.position = this.positions.remove(0);
            Iterator<Sprite> it = this.text.iterator();
            while (it.hasNext()) {
                Sprite sprite = it.next();
                sprite.setPosition(sprite.getX() + this.position.x, sprite.getY() + this.position.y);
            }
            Iterator<Sprite> it2 = this.otherText.iterator();
            while (it2.hasNext()) {
                Sprite sprite2 = it2.next();
                sprite2.setPosition(sprite2.getX() + this.position.x, sprite2.getY() + this.position.y);
            }
            if (!this.positions.isEmpty()) {
                return;
            }
            if (this.braillify) {
                this.braillify = false;
                for (int i = 0; i < 5; i++) {
                    this.positions.add(new Vector2(0.0f, 0.0f));
                }
                this.positions.add(new Vector2(0.0f, 8.0f));
                for (int i2 = 0; i2 < 5; i2++) {
                    this.positions.add(new Vector2(0.0f, 0.0f));
                }
                this.positions.add(new Vector2(0.0f, 8.0f));
                return;
            }
            for (int i3 = 0; i3 < this.numRemove; i3++) {
                this.text.remove(0);
            }
            game.actionStack.remove(this);
        }
    }
}
